package com.etiko.wm.state.client.maingame;

import java.net.URL;

import jmetest.TutorialGuide.HelloMousePick;

import com.jme.bounding.BoundingSphere;
import com.jme.image.Texture;
import com.jme.input.AbsoluteMouse;
import com.jme.input.InputHandler;
import com.jme.input.Mouse;
import com.jme.light.PointLight;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.shape.Box;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jmex.game.state.BasicGameState;

public class ClientMainGameState extends BasicGameState {

	private Box box = null;
	private DisplaySystem display = null;
	private AbsoluteMouse mouse = null;
	private MouseCursorHandler mouseCursorHandler = null;
	private LightState lightState;
	
	public ClientMainGameState(String name) {
		super(name);
		
		// Init the box
		box = new Box("my box", new Vector3f(0, 0, 0), 1, 1, 1);
		box.setModelBound(new BoundingSphere());
	    box.updateModelBound();
	    rootNode.attachChild(box);
	    
	    initZBuffer();
	    
	    // Init Display
	    display = DisplaySystem.getDisplaySystem();
	    
	    // Light
	    PointLight light = new PointLight();
		light.setDiffuse(new ColorRGBA(0.75f, 0.75f, 0.75f, 0.75f));
		light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
		light.setLocation(new Vector3f(0, 100, 0));
		light.setEnabled(true);
		/* Attach the light to a lightState and the lightState to rootNode. */
		lightState = DisplaySystem.getDisplaySystem().getRenderer()
				.createLightState();
		lightState.setEnabled(true);
		lightState.attach(light);
		rootNode.setRenderState(lightState);
	    
	    // Init the mouse
	    mouse = new AbsoluteMouse("The Mouse", display.getWidth(), display
				.getHeight());
		mouse.setLocalTranslation(new Vector3f(mouse.getHotSpotPosition().x,
				mouse.getHotSpotPosition().y, 0));
		mouseCursorHandler = new MouseCursorHandler();
		TextureState ts = display.getRenderer().createTextureState();
		URL cursorLoc = HelloMousePick.class.getClassLoader().getResource(
				"data/cursor/cursor1.png");
		Texture t = TextureManager.loadTexture(cursorLoc,
				Texture.MinificationFilter.NearestNeighborNoMipMaps,
				Texture.MagnificationFilter.Bilinear);
		ts.setTexture(t);
		mouse.setRenderState(ts);
		// Make the mouse's background blend with what's already there
		BlendState as = display.getRenderer().createBlendState();
		as.setBlendEnabled(true);
		as.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
		as
				.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
		as.setTestEnabled(true);
		as.setTestFunction(BlendState.TestFunction.GreaterThan);
		mouse.setRenderState(as);
		rootNode.attachChild(mouse);
		
		rootNode.updateRenderState();
        rootNode.updateWorldBound();
        rootNode.updateGeometricState(0.0f, true);
	}
	
	public void update(float tpf) {
		super.update(tpf);
		box.setLocalTranslation(box.getLocalTranslation().x + tpf, box.getLocalTranslation().y, box.getLocalTranslation().z);
		System.out.println(mouse.getLocalTranslation().x);
		
		if (mouseCursorHandler != null)
			mouseCursorHandler.update(tpf);
	}
	
	public Mouse getMouse() {
		return mouse;
	}
	
	private void initZBuffer() {
		ZBufferState buf = DisplaySystem.getDisplaySystem().getRenderer()
				.createZBufferState();
		buf.setEnabled(true);
		buf.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);
		rootNode.setRenderState(buf);
	}
	
	public class MouseCursorHandler extends InputHandler {

		public MouseCursorHandler() {
			getMouse().registerWithInputHandler(this);
		}

		public void update(float time) {
			super.update(time);

			Vector2f screenPos = new Vector2f();
			// Get the position that the mouse is pointing to
			screenPos.set(getMouse().getHotSpotPosition().x, getMouse()
					.getHotSpotPosition().y);
			// Get the world location of that X,Y value
			Vector3f worldCoords = display.getWorldCoordinates(screenPos, 0);
			System.out.println(screenPos.toString());
		}
	}

}
